package com.pkk.alert.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pkk.alert.common.dto.FeiGeReqDataVo;
import com.pkk.alert.common.feige.FeigeComonBuild;
import com.pkk.alert.entity.feige.FeigeAccount;
import com.pkk.alert.entity.feige.FeigeTemplate;
import com.pkk.alert.service.FeigeAccountService;
import com.pkk.alert.service.FeigeTemplateService;
import java.util.HashMap;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author peikunkun
 * @version V1.0
 * @date 2020-12-01 11:49
 **/
@Component
@Slf4j
public class AlertSupport {

  @Resource
  private FeigeUserEntityServiceImpl feigeUserEntityService;
  @Resource
  private FeigeAccountService feigeAccountService;
  @Resource
  private FeigeTemplateService feigeTemplateService;


  /**
   * 待办事项群组发送
   * <p>
   * {{first}}
   * 事项单号：{{keyword1}}
   * 事项类型：{{keyword2}}
   * 创建人员：{{keyword3}}
   * 通知明细：{{keyword4}}
   * 创建时间：{{keyword5}}
   * {{remark}}
   *
   * @return boolean
   * @Param templateId
   * @author peikunkun
   * @date 2020/12/1 0001 上午 11:49
   * @since
   */
  public boolean sendToDoGroup(String title, String content, String type, String skipUrl) {
    final String templateId = "RV-jBDQSNen0J92hKn5GGE4id6We5Xu2vDgpbXbWe2I";
    FeigeAccount useFeigeAccount = feigeAccountService.getUseFeigeAccount();

    QueryWrapper<FeigeTemplate> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("template_id", templateId);
    FeigeTemplate feigeTemplate = feigeTemplateService.getOne(queryWrapper);
    if (null == feigeTemplate) {
      log.error("【发送消息】-[不知道的模板信息]-templateId：[{}]", templateId);
      return false;
    }

    HashMap<String, FeiGeReqDataVo> param = new HashMap<>();
    param.put("first", FeiGeReqDataVo.builder().value(title).build());
    param.put("keyword1", FeiGeReqDataVo.builder().value("^~^ 爱你 " + DateUtil.now()).build());
    param.put("keyword2", FeiGeReqDataVo.builder().value(type).build());
    param.put("keyword3", FeiGeReqDataVo.builder().value("秘书").build());
    param.put("keyword4", FeiGeReqDataVo.builder().value(content).build());
    param.put("keyword5", FeiGeReqDataVo.builder().value(DateUtil.now()).build());
    param.put("remark", FeiGeReqDataVo.builder().value("I Love You").build());

    if (FeigeComonBuild.buildSendMoreUser(useFeigeAccount.getSecret(), useFeigeAccount.getAppKey(), templateId,
        feigeTemplate.getTemplateContent(), skipUrl, param)) {
      log.info("发送微信通知成功");
      feigeAccountService
          .successFeigeAccount(useFeigeAccount.getId(), feigeUserEntityService.countUser(useFeigeAccount.getId()));
      return true;
    }
    return false;
  }


}
